
// @ts-ignore
import lamejs from 'lamejs'
export const wavToMp3 = function(recorderData:any){
  // new Int16Array(this.recorder.getWAV().buffer) 取录音的buffer
  const channels = 1; //1 单声道 2 立体声
  const sampleRate = 44100; //44.1khz
  const kbps = 128; //128kbps mp3
  var mp3encoder = new lamejs.Mp3Encoder(channels, sampleRate, kbps);
  var mp3Data = [];
  var samples = new Int16Array(recorderData.buffer); // 从源中获取数据
  const sampleBlockSize = 1152; //576的倍数
  for (var i = 0; i < samples.length; i += sampleBlockSize) {
    var sampleChunk = samples.subarray(i, i + sampleBlockSize);
    const mp3buf = mp3encoder.encodeBuffer(sampleChunk);
    if (mp3buf.length > 0) {
      mp3Data.push(mp3buf);
    }
  }
  var mp3buf = mp3encoder.flush();   //finish writing mp3
  if (mp3buf.length > 0) {
    mp3Data.push(new Int8Array(mp3buf));
  }
  var blob = new Blob(mp3Data, {type: 'audio/mp3'});
  return blob;
}
export const blobToBase64 =(blob) =>{
  return new Promise((resolve, reject) => {
    const fileReader = new FileReader();
    fileReader.onload = (e) => {
      resolve(e.target.result);
    };
    fileReader.readAsDataURL(blob);
    fileReader.onerror = () => {
      reject(new Error('blobToBase64 error'));
    };
  });
}